#include <ptgui/ptgui.h>
#include <ptgui/object.h>

static void _ptgui_object_constructor(ptgui_object_t *object)
{
	if (!object) {
		return;
	}

	object->is_static = RT_FALSE;
}

/* Destroys the object */
static void _ptgui_object_destructor(ptgui_object_t *object)
{
	/* nothing */
}

ptgui_type_t * ptgui_type_create(const rt_char_t *type_name, ptgui_type_t *parent_type, rt_int16_t type_size,
								 ptgui_constructor_t constructor, ptgui_destructor_t destructor)
{
	ptgui_type_t *new_type;

	if (!type_name) {
		return RT_NULL;
	}

	new_type = ptgui_malloc(sizeof(ptgui_type_t));
	new_type->name = rt_strdup(type_name);
	new_type->size = type_size;
	new_type->constructor = constructor;
	new_type->destructor = destructor;

	if (!parent_type) {
		new_type->hierarchy_depth = 0;
		new_type->hierarchy = RT_NULL;
	}
	else {
		/* Build the type hierarchy */
		new_type->hierarchy_depth = parent_type->hierarchy_depth + 1;
		new_type->hierarchy = ptgui_malloc(sizeof(ptgui_type_t *) * new_type->hierarchy_depth);

		new_type->hierarchy[0] = parent_type;
		rt_memcpy(new_type->hierarchy + 1, parent_type->hierarchy,
				  parent_type->hierarchy_depth * sizeof(ptgui_type_t *));
	}

	return new_type;
}

void ptgui_type_destroy(ptgui_type_t *type)
{
	if (!type) {
		return;
	}

	if (type->hierarchy) {
		ptgui_free(type->hierarchy);
	}

	ptgui_free(type->name);
	ptgui_free(type);
}

void ptgui_type_object_construct(ptgui_type_t *type, ptgui_object_t *object)
{
	int i;

	if (!type || !object) {
		return;
	}

	/* Call the constructors */
	for (i = type->hierarchy_depth - 1; i >= 0; i--) {
		if (type->hierarchy[i]->constructor) {
			type->hierarchy[i]->constructor(object);
		}
	}
	if (type->constructor) {
		type->constructor(object);
	}
}

void ptgui_type_destructors_call(ptgui_type_t *type, ptgui_object_t *object)
{
	int i;

	if (!type || !object) {
		return;
	}

	if (type->destructor) {
		type->destructor(object);
	}
	for (i = 0; i < type->hierarchy_depth; i++) {
		if (type->hierarchy[i]->destructor) {
			type->hierarchy[i]->destructor(object);
		}
	}
}

rt_bool_t ptgui_type_inherits_from(ptgui_type_t *type, ptgui_type_t *parent)
{
	int i;

	if (!type || !parent) {
		return RT_FALSE;
	}

	if (type == parent) {
		return RT_TRUE;
	}

	for (i = 0; i < type->hierarchy_depth; i++) {
		if (type->hierarchy[i] == parent) {
			return RT_TRUE;
		}
	}

	return RT_FALSE;
}

ptgui_type_t * ptgui_type_parent_type_get(ptgui_type_t *type)
{
	if (!type || !type->hierarchy) {
		return RT_NULL;
	}

	return type->hierarchy[0];
}

const rt_char_t * ptgui_type_name_get(ptgui_type_t *type)
{
	if (!type) {
		return RT_NULL;
	}

	return type->name;
}

struct ptgui_object_information
{
	rt_uint32_t	objs_number;
	rt_uint32_t	allocated_size;
	rt_uint32_t	max_allocated;
};
struct ptgui_object_information obj_info =
{
	0,
	0,
	0
};

/**
 * @brief Creates a new object: it calls the corresponding constructors (from the constructor of the base class to the
 * constructor of the more derived class) and then sets the values of the given properties
 * @param object_type the type of object to create
 * @return Returns the new Etk_Object of type @a object_type
 */
ptgui_object_t * ptgui_object_create(ptgui_type_t *object_type)
{
	ptgui_object_t *new_object;

	if (!object_type) {
		return RT_NULL;
	}

	new_object = ptgui_malloc(object_type->size);
	if (new_object == RT_NULL) {
		return RT_NULL;
	}

	obj_info.objs_number ++;
	obj_info.allocated_size += object_type->size;
	if (obj_info.allocated_size > obj_info.max_allocated) {
		obj_info.max_allocated = obj_info.allocated_size;
	}

	new_object->type = object_type;
	new_object->is_static = RT_FALSE;

	ptgui_type_object_construct(object_type, new_object);

	return new_object;
}

/**
 * @brief Destroys the object: it first sets the weak-pointers to RT_NULL, emits the "destroyed" signal, and then
 * queues the object in the list of objects to free. Thus, the destructors will only be called at the beginning of the
 * next main loop iteration (from the destructor of the more derived class to the destructor of the ultimate base class).
 * @param object the object to destroy
 * @warning You should not assume that this function will call directly the destructors of the object!
 */
void ptgui_object_destroy(ptgui_object_t *object)
{
	if (!object || object->is_static == RT_TRUE) {
		return;
	}

	obj_info.objs_number --;
	obj_info.allocated_size -= object->type->size;

	/* call destructor */
	RT_ASSERT(object->type != RT_NULL);
	ptgui_type_destructors_call(object->type, object);

	/* release object */
	ptgui_free(object);
}

/**
 * @internal
 * @brief Gets the type of a ptgui_object
 * @return Returns the type of a ptgui_object
 */
ptgui_type_t * ptgui_object_type_get(void)
{
	static ptgui_type_t *object_type = RT_NULL;

	if (!object_type) {
		object_type = ptgui_type_create("object", RT_NULL, sizeof(ptgui_object_t),
										PTGUI_CONSTRUCTOR(_ptgui_object_constructor),
										PTGUI_DESTRUCTOR(_ptgui_object_destructor));
	}

	return object_type;
}

/**
 * @brief Checks if @a object can be cast to @a type.
 * If @a object doesn't inherit from @a type, a warning is displayed in the console but the object is returned anyway.
 * @param object the object to cast
 * @param type the type to which we cast the object
 * @return Returns the object
 * @note You usually do not need to call this function, use specific macros instead (ETK_IS_WIDGET() for example)
 */
ptgui_object_t * ptgui_object_check_cast(ptgui_object_t *object, ptgui_type_t *type)
{
	if (!object) {
		return RT_NULL;
	}

	if (!ptgui_type_inherits_from(object->type, type)) {
		rt_kprintf("Invalid cast from \"%s\" to \"%s\"\n", ptgui_type_name_get(object->type),
				   ptgui_type_name_get(type));
	}

	return object;
}

/**
 * @brief Gets the type of the object
 * @param object an object
 * @return Returns the type of @a object (RT_NULL on failure)
 */
ptgui_type_t * ptgui_object_object_type_get(ptgui_object_t *object)
{
	if (!object) {
		return RT_NULL;
	}

	return object->type;
}
